
window.onload=function()
{
    // blick()

    //判断登录
    pandLogin()

}

function blick()
{
    
    var container=document.getElementById("container")
    var clickList={
        "记录":"cl('templates/html/log.php')",
        "中转站":"cl('中转站/index.html')",
        "资料堆":"cl('资料堆/index.html')",
        "音乐":"cl('音乐播放器/music.php')",
        "火车时刻表":"cl('hc/index.html')",
        "行程安排":"cl('行程安排/行程安排.html')",
        "计时器":"cl('计时器/countTime.html')",
        "校对":"cl('校对/校对.html')",
        "编解码":"cl('编解码/index.php')",
        "阳历阴历计算":"cl('阳历阴历日期计算/index.html')",
        "编辑器":"cl('代码编辑/editCode.html')",
        "服务器":"cl('http://QuickConnect.cn/TH2009')",
        "测试":"cl('test.html')",
        
        }


    container.innerHTML="<div class='title'><span>导航</span><span id='logout'>退出</span></div>"
    for(var key in clickList)
    {
        var div=document.createElement("div")
        div.className="ele"
        div.innerText=key
        div.setAttribute("onclick",clickList[key])
        container.appendChild(div)
    }


    var logout=document.getElementById("logout")
    logout.onclick=function()
    {
        setCookie("th=no")
        setCookie("passWord=no")
        pandLogin()
    }
}

function cl(url)
{   
    var cUrl=""
    var newUrl=""
    if(url.includes("http:"))
    { 
      var userInput = prompt("请输入您的名字:", "");
      if(userInput!="w2008")
      {
        alert("密码错误")
        return
      }
      newUrl=url

    }
    else
    {
      cUrl=window.location.href
      newUrl=cUrl+url
    }
    window.location.href=newUrl
}


//登录
function pandLogin()
{

   var usr=getCookie("usr")
   var passWord=getCookie("passWord")


   if(usr!="th"||passWord!="w2009")
   {
        var container=document.getElementById("container")
        container.innerHTML="<div id='login'><div id='tips'>请先登录</div><div ><span>\
        账号:</span><input id='usr' type='text' placeholder='usr' autocomplete='off'></div><div><span>\
        密码:</span><input id='passWord' type='text' placeholder='passWord'\
        onclick='ableYes()' autocomplete='off'>\
        </div><div><button onclick=login()>确定</button></div></div>"
        if(usr!="th")
        {
            return "usr"
        }
        else
        {
            return "passWord"
        }
   }
   else 
   {
     blick()
   }
   return "no"
}

//登录
function login()
{
    var usr=document.getElementById("usr")
    var passWord=document.getElementById("passWord")

    setCookie("usr="+usr.value)
    setCookie("passWord="+passWord.value)
    if(pandLogin()=="usr")
    {
        alert("账号错误")
    }
    else if(pandLogin()=="passWord")
    {
        alert("密码错误")
    }
    
}
//设置cookie
function setCookie(string)
{
    var date=new Date()
    date.setDate(date.getDate() + 5);
    document.cookie=string+";"+"expires="+date.toUTCString();
}

// 获取cookie
function getCookie(keyIndex)
{
    
    if(document.cookie.length==0)
    {
        return "no"
    }
    else
    {
        var cookies=document.cookie.split(";")
    }
    
    for(var i=0;i<cookies.length;i++)
    {
        var sp=cookies[i].split("=")
        var key=sp[0].trim()
        var value=sp[1].trim()
        if(keyIndex==key)
        {
            return value
        }
    }
    return "no"
    
}


function linkKey()
{
    document.addEventListener("keydown",Fun)
    function Fun(e)
    {
         if(e.key=="Enter")
        {
            login()
            setTimeout(function()
            {
                document.removeEventListener(Fun)
            },1000)
            
        }

    }
}
function ableYes()
{
    linkKey()
}